<?php 
if ( ! function_exists('categoriasRecursivasCheckbox')){
	
	function categoriasRecursivasCheckbox($field_name, $categorias, $nodeCategories = NULL){
		if($categorias){
	 		foreach($categorias as $categoria){
				$selected = "";			
				if($nodeCategories != NULL && isCategorySelected($categoria->id, $nodeCategories)){
					$selected = "checked='checked'";
				}
	 			?>
	 			<li>
	 				<input <?php echo $selected; ?> type="checkbox" name="<?php echo $field_name; ?>[]" value="<?php echo $categoria->id;?>" />
					<span><?php echo $categoria->name;?></span> <?php if($categoria->visible == 0) echo "<span class='no_visible'>(No visible)</span>"; ?>
	 				
					<?php if(isset($categoria->categories) && count($categoria->categories)>0) : ?>
		 				<ul>
		 				<?php categoriasRecursivasCheckbox($field_name, $categoria->categories, $nodeCategories); ?>
		 				</ul>
		 			<?php endif; ?>
	 			</li>
	 			<?php 
	 		}
		}
	}
}

if( ! function_exists('isCategorySelected')){
	function isCategorySelected($id, $categorias){
		if(isset($categorias)){
			foreach($categorias as $node_category){
				if($id == $node_category->id){
					return TRUE;
				}
			}	
		}
	
		return FALSE;
	}	
}
?>

<?php echo form_label($field_title, $field_name); ?>
<ul class="categories">
	<?php categoriasRecursivasCheckbox($field_name, $categories, $selected_categories); ?>
</ul>